package com.example.licensingServer.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.licensingServer.config.ServiceConfig;
import com.example.licensingServer.model.License;
import com.example.licensingServer.repository.LicenseRepository;

@Service
public class LicenseService {
	@Autowired
	private LicenseRepository licenseRepository;
	
	@Autowired
	ServiceConfig config;
	
	public License getLicense(String organizationId,String licenseId){
		License license = 
			licenseRepository.findByOrganizationIdAndLicenseId(organizationId, licenseId);
		if(license != null) {
			license.setComment(config.getExampleProperty());
		}
		return license;
	}
	
	public List<License> getLicensesByOrganizationId(String organizationId){
		return licenseRepository.findByOrganizationId(organizationId);
	}
	
	public void saveLicense(License license) {
		license.setLicenseId(UUID.randomUUID().toString());
		licenseRepository.save(license);
	}
}
